package com.wtwd.device.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * 利用mybatisPlus 自动填充数据
 *
 * @author zdl
 * @Description
 * @create 2021-06-09 13:37
 */
@Component
public class MyBatisMetaHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        long now = 0;
        if (metaObject.getValue("createTime") == null) {
            now = System.currentTimeMillis();
            setFieldValByName("createTime", now, metaObject);
        }
        if (metaObject.getValue("updateTime") == null) {
            setFieldValByName("updateTime", now != 0 ? now : System.currentTimeMillis(), metaObject);
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.getValue("updateTime") == null) {
            setFieldValByName("updateTime", System.currentTimeMillis(), metaObject);
        }
    }
}
